// 实现strncasecmp
#include <stdio.h>
#include <stdlib.h>

#define SIZE 50

int my_strncasecmp(char *str01, char *str02, int n);
char *str_convert(char *str);

int main(int argc, char **argv)
{
    char str1[SIZE] = "";
    char str2[SIZE] = "";
    int n = 0;

    // 人机交互 获取键盘输入
    printf("输入\n");
    scanf("%s", str1);
    scanf("%s", str2);
    scanf("%d", &n);

    // 比较两个字符串
    if (my_strncasecmp(str1, str2, n) > 0)
    {
        printf("大于\n");
    }
    else if (my_strncasecmp(str1, str2, n) < 0)
    {
        printf("小于\n");
    }
    else 
    {
        printf("等于\n");
    }


    return 0;
}

int my_strncasecmp(char *str01, char *str02, int n)
{
    int i = 0;
    char *str1 = str_convert(str01);
    char *str2 = str_convert(str02);

    for (i = 0; i < n; i++)
    {
        if (str1[i] == str2[i] && str1[i] != '\0')
        {
            continue;
        }
        else if (str1[i] > str2[i])
        {
            goto return0;
        }
        else if (str1[i] < str2[i])
        {
            goto return1;
        }
        else 
        {
            goto return2;
        }
    }


return2:
    free(str1);
    free(str2);
    return 0;
return0:
    free(str1);
    free(str2);
    return 1;
return1:
    free(str1);
    free(str2);
    return -1;

}

char *str_convert(char *str)
{
    int n = 0;
    char *str_p = NULL;

    // 测量传入字符串长度
    while (str[n] != '\0')
    {
        n++;
    }

    // 申请空间
    str_p = calloc(n + 1, sizeof(char));
    if (str_p == NULL)
    {
        perror("calloc");
        goto error0;
    }

    // 字符串转换
    n = 0;
    while (str[n] != '\0')
    {
        if (str[n] >= 'A' && str[n] <= 'Z')
        {
            str_p[n] = str[n] + 32;
        }
        else 
        {
            str_p[n] = str[n];
        }
        
        n++;
    }

    return str_p;

error0:
    return (void *)1;

}